{include file='pub/header'/}
<title>商品列表</title>
<style>
    td a{
        display: inline-block;
    }
</style>
</head>
<body class="pos-r">
<div>
    <div class="page-container">
        <div class="text-c">
            <form action="" method="get">
            商品名称：<input type="text" name="name" value="{$name||default=''}" id="" placeholder=" 商品名称" style="width:250px" class="input-text">
            <button name="" class="btn btn-success" type="submit"><i class="Hui-iconfont">&#xe665;</i> 搜商品
            </button>
            </form>
        </div>
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <a href="javascript:;" onclick="datadel()" class="btn btn-danger radius">
                    <i class="Hui-iconfont">&#xe6e2;</i> 批量删除
                </a>
                <a class="btn btn-primary radius" onclick="product_add('添加商品','productAdd.html')" href="javascript:;">
                    <i class="Hui-iconfont">&#xe600;</i> 添加商品
                </a>
                <a class="btn btn-primary radius" title="导出商品" onclick="product_download()" href="javascript:;">
                    <i class="Hui-iconfont Hui-iconfont-down"></i> 商品导出
                </a>
                <a class="btn btn-primary radius" title="上传商品" onclick="loadProductExcel()" href="javascript:;">
                    <i class="Hui-iconfont Hui-iconfont-upload"></i> 上传商品
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(1)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>首页推荐
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(2)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>热销商品
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(3)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>库存不足
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(4)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>库存充足
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(5)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>已上架
                </a>
                <a href="javascript:;" class="btn btn-primary radius" onclick="listCon(6)">
                    <i class="Hui-iconfont Hui-iconfont-search"></i>未上架
                </a>
            </span>
            <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
               href="javascript:location.replace(location.href);" title="刷新">
                <i class="Hui-iconfont">&#xe68f;</i>
            </a>
        </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-bg table-sort">
                <thead>
                <tr class="text-c">
                    <th width="20"><input name="" type="checkbox" value=""></th>
                    <th width="40">ID</th>
                    <th width="60">缩略图</th>
                    <th width="40">产品名称</th>
                    <!--<th width="40">产品分类</th>-->
                    <th width="60">返利</th>
                    <th width="30">运费</th>
                    <th width="20">首页显示</th>
                    <th width="20">热销产品</th>
                    <th width="20">新用户</th>
                    <th width="20">特价</th>
                    <th width="20">新品</th>
                    <th width="50">库存状态</th>
                    <th width="30">排序</th>
                    <th width="200">规格</th>
                    <th width="100">操作</th>
                </tr>
                </thead>
                <tbody>
                {foreach $goodsList as $good}
                <tr class="text-c va-m" {if $good.is_limit eq 1} style="background-color: red;" {/if}>
                    <td><input name="id" type="checkbox" value="{$good.id}"></td>
                    <td>{$good.id}</td>
                    <td>
                        <a onClick="picture_show('查看图片', '{$good.thumb_img}')" data-src="" href="javascript:;">
                            <img width="60" class="product-thumb" src="{$good.thumb_img}">
                        </a>
                    </td>
                    <td>
                        {$good.name}
                    </td>
                    <!--<td>-->
                        <!--{$good.cate_name}-->
                    <!--</td>-->
                    <td>
                        <p><div style="width: 120px;">代理商：<input onblur="saveCommission(this)" class="input input-text" value="{$good.agent_commission}" data-type="1" data-pid="{$good.id}" type="text" style="width: 50px;">%</div></p>
                        <p><div style="width: 120px;">销售：<input onblur="saveCommission(this)"  class="input input-text" value="{$good.salesman_commission}" type="text" data-type="2" data-pid="{$good.id}" style="width: 50px;">%</div></p>
                        <p><div style="width: 120px;">兼职代理：<input onblur="saveCommission(this)"  class="input input-text" value="{$good.parttime_commission}" type="text" data-type="3" data-pid="{$good.id}" style="width: 50px;">%</div></p>
                    </td>
                    <td>
                        <input type="text" class="input input-text" style="width: 50px;" onblur="saveShipping(this)" value="{$good.send_fee}" data-pid="{$good.id}">
                    </td>
                    <td class="td-status">
                        <!--<div class="switch switch-small">-->
                            <input type="checkbox" onchange="setIndex('{$good.id}', this)" {if condition="$good.is_index eq 1"}checked{/if} />
                        <!--</div>-->
                    </td>
                <td class="td-status">
                    <!--<div class="switch switch-small">-->
                    <input type="checkbox" onchange="setHot('{$good.id}', this)" {if condition="$good.is_hot eq 1"}checked{/if} />
                    <!--</div>-->
                </td>
                <td class="td-status">
                    <!--<div class="switch switch-small">-->
                    <input type="checkbox" onchange="setStatus('{$good.id}', this, 'new_user')" {if condition="$good.new_user eq 1"}checked{/if} />
                    <!--</div>-->
                </td>
                <td class="td-status">
                    <!--<div class="switch switch-small">-->
                    <input type="checkbox" onchange="setStatus('{$good.id}', this, 'tejia')" {if condition="$good.tejia eq 1"}checked{/if} />
                    <!--</div>-->
                </td>
                <td class="td-status">
                    <!--<div class="switch switch-small">-->
                    <input type="checkbox" onchange="setStatus('{$good.id}',this, 'xinpin')" {if condition="$good.xinpin eq 1"}checked{/if} />
                    <!--</div>-->
                </td>
                <td class="td-status">
                    {if condition="$good.is_limit eq 1"}
                    库存不足
                    {else}
                    库存正常
                    {/if}
                </td>
                <td>
                    <input class="input input-text size-MINI" type="text" value="{$good.ord}" onblur="changeOrder(this, '{$good.id}')">
                </td>
                <td>
                    <div>
                        <table class="table table-border table-bordered">
                            <tr>
                                <td width="50">规格</td>
                                <td width="100">单价</td>
                                <td width="100">件价</td>
                                <td width="50">库存</td>
                                <td width="50">操作</td>
                            </tr>
                            {foreach $good.productAttr as $item}
                            <tr>
                                <td><span style="font-size: 12px;">{$item.prop_value_name}</span></td>
                                <td><input style="font-size: 12px;" type="text" class="input input-text price_one" value="{$item.price_one}"/></td>
                                <td><input style="font-size: 12px;" type="text" class="input input-text price_comb" value="{$item.price_comb}"/></td>
                                <td><input style="font-size: 12px;" type="text" class="input input-text remain" value="{$item.remain}"/></td>
                                <td><button type="button" onclick="setProp(this, '{$item.id}')" class="btn btn-primary radius size-MINI">保存</button></td>
                            </tr>
                            {/foreach}
                        </table>
                    </div>

                </td>
                    <td class="td-manage">
                        <a style="text-decoration:none" class="ml-5"
                           onClick="product_edit('产品编辑','{:url(\'Product/productEdit\', [\'product_id\'=>$good.id])}')"
                           href="javascript:;" title="编辑">
                            <i class="Hui-iconfont">&#xe6df;</i>编辑
                        </a>
                        {if $good.is_up eq 1}
                        <a style="text-decoration:none" class="ml-5"
                           onClick="product_down('{$good.id}')"
                           href="javascript:;" title="下架">
                            <i class="Hui-iconfont Hui-iconfont-xiajia"></i>商品下架
                        </a>
                        {/if}

                        {if $good.is_up eq 0}
                        <a style="text-decoration:none" class="ml-5"
                           onClick="product_up('{$good.id}')"
                           href="javascript:;" title="上架"><i class="Hui-iconfont Hui-iconfont-shangjia"></i>商品上架
                        </a>
                        {/if}

                        <a style="text-decoration:none" class="ml-5" onClick="product_del(this,'{$good.id}')"
                           href="javascript:;" title="删除">
                            <i class="Hui-iconfont">&#xe6e2;</i>删除
                        </a>
                    </td>
                </tr>
                {/foreach}
                </tbody>
            </table>
            {$page}
        </div>
    </div>
</div>

{include file='pub/footer'/}
<script type="text/javascript">
    // $('.table-sort').dataTable({
    //     "ordering":false,
    //     "bStateSave": true,//状态保存
    // });
    //导出商品
    function product_download() {
        location.href = "{:url('download')}";
    }
    //导入商品信息
    function loadProductExcel(){
        window.parent.layer_show("导入商品", "{:url('loadExcel')}");
    }

    function saveShipping(o){
        var pid = $(o).data("pid");
        var com = $(o).val();
        if(isNaN(com) || com == ""){
            layer.msg("运费不能为空");
            return ;
        }
        $.post("{:url('saveShipping')}", {pid:pid, com:com}, function (res) {
            if(res.code == 1){
                layer.msg("保存成功");
            }else{
                layer.msg(res.msg);
            }
        });

    }
    function saveCommission(o){
        var type = $(o).data("type");
        var pid = $(o).data("pid");
        var com = $(o).val();
        if(isNaN(com) || com == ""){
            layer.msg("返利比率不能为空");
            return ;
        }
        $.post("{:url('saveCommission')}", {type:type, pid:pid, com:com}, function (res) {
            if(res.code == 1){
                layer.msg("保存成功");
            }else{
                layer.msg(res.msg);
            }
        });
    }

    function setProp(o, prop_id){
        var price_one = $(o).parent().parent().find(".price_one").val();
        var price_comb = $(o).parent().parent().find(".price_comb").val();
        var remain = $(o).parent().parent().find(".remain").val();
        $.post("{:url('Product/setProp')}", {price_comb:price_comb, price_one:price_one, remain:remain, prop_id:prop_id}, function (res) {
            layer.msg(res.msg);
        });
    }

    function changeOrder(o, pid){
        var order = parseInt($(o).val());
        if(typeof order !="number"){
            order = 0;
        }
        $.post('{:url("Product/setOrder")}', {order:order, pid:pid}, function(res){
            layer.msg("设置成功");
        });
    }
    function product_down(id){
        layer.confirm("确定下架该商品？", function () {
            $.post("{:url('upAndDown')}", {id:id, is_up:0}, function (res) {
                if(res.code == 1){
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.reload();
                    }, 666);
                }else{
                    layer.msg(res.msg);
                }
            });
        })
    }

    function product_up(id){
        layer.confirm("确定上架该商品？", function () {
            $.post("{:url('upAndDown')}", {id:id, is_up:1}, function (res) {
                if(res.code == 1){
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.reload();
                    }, 666);
                }else{
                    layer.msg(res.msg);
                }
            });
        })

    }



    function listCon(status) {
        location.href = location.origin+location.pathname+"?con="+status;
    }

    function setIndex(product_id, o){
        var is_index = $(o).prop('checked')?1:0;
        $.post("{:url('setIndex')}", {product_id:product_id, is_index:is_index}, function (res) {
            if(res.code == 1){
                layer.msg('操作成功');
            }else{
                layer.msg(res.msg);
                if(is_index == 0){
                    $(o).prop('checked', false);
                }else{
                    $(o).prop('checked', true);
                }
            }
        });
    }
    function setHot(product_id, o){
        var is_index = $(o).prop('checked')?1:0;
        $.post("{:url('setHot')}", {product_id:product_id, is_hot:is_index}, function (res) {
            if(res.code == 1){
                layer.msg('操作成功');
            }else{
                layer.msg(res.msg);
                if(is_index == 0){
                    $(o).prop('checked', false);
                }else{
                    $(o).prop('checked', true);
                }
            }
        });
    }
    function setStatus(product_id, o, name){
        var is_index = $(o).prop('checked')?1:0;
        $.post("{:url('setStatus')}", {product_id:product_id, is_hot:is_index, name:name}, function (res) {
            if(res.code == 1){
                layer.msg('操作成功');
            }else{
                layer.msg(res.msg);
                if(is_index == 0){
                    $(o).prop('checked', false);
                }else{
                    $(o).prop('checked', true);
                }
            }
        });
    }

    /*产品-添加*/
    function product_add(title, url) {
        var index = parent.layer.open({
            type: 2,
            title: title,
            content: url
        });
        parent.layer.full(index);
    }

    /*图片-查看*/
    function picture_show(title, url, id) {
        var imgsurl = url.split(',');
        var content = '';
        $.each(imgsurl, function (i, v) {
            content += "<img style='width: 120px;' src='" + v + "'/>";
        });
        var index = layer.open({
            type: 1,
            title: title,
            content: content
        });
        // layer.full(index);
    }

    /*产品-编辑*/
    function product_edit(title, url, id) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }

    /*产品-删除*/
    function product_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.post("{:url('Product/delData')}", {idstr:id}, function (res) {
                if(res.code == 1){
                    layer.msg('删除成功');
                    $(obj).parents('tr').remove();
                }else{
                    layer.msg(res.msg);
                }
            });
        });
    }
    //删除选中产品
    function datadel() {
        var $ids = $("input[name='id']:checked");
        if($ids.length<1){
            layer.alert('请选中后在操作');
            return false;
        }
        var idarr = new Array();
        $.each($ids, function (i, v) {
            idarr.push($(v).val());
        });
        var idStr = idarr.toString();
        layer.confirm('确认删除选中项？', function () {
            $.post("{:url('Product/delData')}", {idstr:idStr}, function (res) {
                if(res.code == 1){
                    layer.msg('删除成功');
                    $ids.parents('tr').remove();
                }else{
                    layer.msg(res.msg);
                }
            });
        });
    }
</script>
</body>
</html>